package com.tongfusoft.document.manager.account.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;

import com.tongfusoft.da.pagehelper.PageInfo;
import com.tongfusoft.da.template.service.impl.DataTemplateServiceImpl;
import com.tongfusoft.document.entity.AccountHasUsers;
import com.tongfusoft.document.entity.Accounts;
import com.tongfusoft.document.entity.Users;
import com.tongfusoft.document.manager.account.service.IDocumentAccountService;
import com.tongfusoft.exception.ServiceException;
import com.tongfusoft.spring.web.core.data.User;
import com.tongfusoft.utils.UIDGeneratorService;

@Service("documentAccountService")
public class DocumentAccountServiceImpl extends DataTemplateServiceImpl
		implements IDocumentAccountService {

	@Override
	public void createAccount(Accounts account, Users user) {
		if (super.selectList("listAcountsAll", account).size() > 0) {

			throw new ServiceException("repeat_error");
		}
		account.setCreatedBy(user.getUserId());
		account.setCreatedDate(new java.sql.Timestamp(new java.util.Date().getTime()));
		account.setLastUpdBy(user.getUserId());
		account.setLastUpdDate(new java.sql.Timestamp(new java.util.Date().getTime()));
		account.setAccountId(UIDGeneratorService.getUUID());
		account.setStatus("1");
		this.insert("addAcount", account);
	}

	@Override
	public void deleteAccount(Accounts account, Users user) {
		account.setLastUpdBy(user.getUserId());
		account.setLastUpdDate(new java.sql.Timestamp(new java.util.Date().getTime()));
		this.delete("delAccount", account);
	}

	@Override
	public void updateAccount(Accounts account, Users user) {
		if (super.selectList("listAcountsAll", account).size() > 0) {

			throw new ServiceException("repeat_error");
		}
		account.setLastUpdBy(user.getUserId());
		account.setLastUpdDate(new java.sql.Timestamp(new java.util.Date().getTime()));
		this.update("updateAccount", account);
	}

	@Override
	public List<Accounts> accountList() {
		return this.selectList("allAccounts");
	}
    
	@Override
	public PageInfo<Accounts> accountPageTable(int pageIndex, int pageSize,
			Accounts account) {
		return super.findPage("listAcounts", account, pageSize, pageIndex);
	}

	@Override
	public Accounts detailAccount(Accounts account) {
		return (Accounts) this.selectOne("listAcounts", account);
	}

	@Override
	public List<Accounts> accountListById(Users user) {
		return this.selectList("allAccountsById",user);
	}

	@Override
	public List<AccountHasUsers> getAccountHasUser(
			AccountHasUsers accountHasUsers) {
		return this.selectList("getAccountHasUsers", accountHasUsers);
	}

}
